package io.github.wjehovah.yyn.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;

import   io.github.wjehovah.yyn.bean.VideoBean.VideoDetail;

import java.util.List;

import io.github.wjehovah.yyn.R;
import io.github.wjehovah.yyn.bean.VideoBean;


/**
 * Created by yingchen on 2015/8/3.
 */
public class VideoAdapter extends BaseAdapter {
    private VideoBean data;
    private Context context;
    private BitmapUtils mBitmapUtils;
    public VideoAdapter(VideoBean data, Context context) {
        this.data = data;
        this.context = context;
        mBitmapUtils = new BitmapUtils(context);
    }

    @Override
    public int getCount() {
        return data.list.size();
    }

    @Override
    public VideoDetail getItem(int position) {
        return data.list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.video_item, null);

            holder.usericon = (ImageView) convertView.findViewById(R.id.iv_top_item_usericon);
            holder.username = (TextView) convertView.findViewById(R.id.tv_top_item_username);
            holder.passtime = (TextView) convertView.findViewById(R.id.tv_top_item_passtime);
            holder.content = (TextView) convertView.findViewById(R.id.tv_content_item_detail);

            holder.video = (FrameLayout) convertView.findViewById(R.id.fl_content_item_video_picture);

            holder.love = (TextView) convertView.findViewById(R.id.tv_foot_love);
            holder.hate = (TextView) convertView.findViewById(R.id.tv_foot_hate);
            holder.transmit = (TextView) convertView.findViewById(R.id.tv_foot_transmit);
            holder.comment = (TextView) convertView.findViewById(R.id.tv_foot_commnet);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //获取对应位置数据
        VideoDetail videoDetail = getItem(position);

        //设置头像    videoDetail.profile_image;
        if(!TextUtils.isEmpty(videoDetail.profile_image)){
            //有用户头像连接
            mBitmapUtils.display(holder.usericon,videoDetail.profile_image);
        }else{
            //没有用户头像连接  设置默认头像
            holder.usericon.setImageResource(R.drawable.default_icon);
        }


        //设置用户名
        holder.username.setText(videoDetail.screen_name);
        //设置发表时间
        holder.passtime.setText(videoDetail.passtime);
        //设置内容
        holder.content.setText(videoDetail.text);

        //设置视频 后期---// TODO: 2015/8/3-----
        TextView tvUrl = new TextView(context);
        tvUrl.setText(videoDetail.videouri);
        tvUrl.setTextSize(20);
        tvUrl.setGravity(Gravity.CENTER);
        holder.video.removeAllViews();
        holder.video.addView(tvUrl);

        //设置点赞
        holder.love.setText(videoDetail.love);
        //设置踩
        holder.hate.setText(videoDetail.hate);

        //设置转发  //数据存在问题  没有转发
        holder.transmit.setText(videoDetail.favourite);

        //设置评论
        holder.comment.setText(videoDetail.comment);
        return convertView;
    }

    static class ViewHolder {
        public ImageView usericon;
        public TextView username;
        public TextView passtime;
        public TextView content;
        public FrameLayout video;
        public TextView love;
        public TextView hate;
        public TextView transmit;
        public TextView comment;
    }
}
